<template>
  <div class="hello">
    <div>
      date:{{orderByObj}}
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column width="180">
        <template slot="header" slot-scope="scope">
          <div @click="thClick('date')">日期-{{oy.date}}</div>
        </template>
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.date }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100">
        <template slot="header" slot-scope="scope">
          <div @click="thClick('name')">姓名-{{oy.name}}</div>
        </template>
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100">
        <template slot="header" slot-scope="scope">
          <div @click="thClick('age')">年龄-{{oy.age}}</div>
        </template>
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.age }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100">
        <template slot="header" slot-scope="scope">
          <div @click="thClick('gender')">性别-{{nameState}}</div>
        </template>
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.gender }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100">
        <template slot="header" slot-scope="scope">
          <div @click="thClick('price')">价格-{{nameState}}</div>
        </template>
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址"
        :formatter="formatter">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    name: 'HelloWorld',
    data() {
      return {
        msg: 'Welcome to Your Vue.js App',
        tableData: [
          {
            date: '2016-05-02',
            name: '王大虎',
            age: 20,
            gender: 'male',
            price: '100.00',
            address: '上海市普陀区金沙江路 1518 弄'
          },
          {
            date: '2016-05-04',
            name: '王二虎',
            age: 20,
            gender: 'male',
            price: '100.00',
            address: '上海市普陀区金沙江路 1517 弄'
          },
          {
            date: '2016-05-01',
            name: '王三虎',
            age: 20,
            gender: 'male',
            price: '100.00',
            address: '上海市普陀区金沙江路 1519 弄'
          },
          {
            date: '2016-05-03',
            name: '王四虎',
            age: 20,
            gender: 'male',
            price: '100.00',
            address: '上海市普陀区金沙江路 1516 弄'
          }
        ],
        dateState: 0,
        nameState: 0,
        orderByKey: {
          1: 'desc',
          2: 'asc'
        },
        oy: {
          date: 0,
          name: '0'
        }
      }
    },
    mounted() {
      this.orderByObj = {}
    },
    methods: {
      columnState(key) {
        let currentKey = this.orderByObj[key]
        let state = ''
        if (currentKey) {
          state = currentKey === 'desc' ? '↓' : '↑'
        }
        return state
      },
      formatter(row, column) {
        return row.address
      },
      thClick(key) {
        console.log('dateClick', key)
        let currentKey = this.orderByObj[key]
        if (!currentKey) {
          this.orderByObj[key] = 'desc'
          this.oy[key] = 1
        } else {
          if (currentKey === 'asc') {
            this.oy[key] = 0
            delete this.orderByObj[key]
          } else {
            this.orderByObj[key] = 'asc'
            this.oy[key] = 2
          }
        }
        console.log('this.orderByObj', this.orderByObj)
        let strs = []
        for (let k in this.orderByObj) {
          strs.push(`${k} ${this.orderByObj[k]}`)
        }
        console.log('strs', strs.join(','))
        // switch (key) {
        //   case 'date':
        //     this.dateState += 1
        //     if (this.dateState > 2) {
        //       this.dateState = 0
        //     }
        //     break
        //   case 'name':
        //     this.nameState += 1
        //     if (this.nameState > 2) {
        //       this.nameState = 0
        //     }
        //     break
        // }
        // this.spellOrderByStr()
      },
      spellOrderByStr() {
        let jsonStr = []
        let dateOrder = this.orderByKey[this.dateState]
        let nameOrder = this.orderByKey[this.nameState]
        jsonStr.push(dateOrder ? ` date ${dateOrder}` : null)
        jsonStr.push(nameOrder ? ` date ${nameOrder}` : null)
        console.log(jsonStr)
      }
    },
    computed: {
      orderColumn() {
        // this.orderByObj
        return {}
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  h1, h2 {
    font-weight: normal;
  }

  ul {
    list-style-type: none;
    padding: 0;
  }

  li {
    display: inline-block;
    margin: 0 10px;
  }

  a {
    color: #42b983;
  }
</style>
